package com.plugins.http;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 我关注的用户
 * @author user
 *
 */
public class MyWbFollows {

	
	
	public static List<String> getFollows(Integer pageSize) throws InterruptedException{
		RequestConfig config = RequestConfig.custom()
				//.setProxy(new HttpHost("127.0.0.1", 8888))
				.setCookieSpec(CookieSpecs.STANDARD)
				.setRedirectsEnabled(true)
				.build();
		for(Integer page = 1; page <= pageSize; page++){
			HttpClientContext httpContext = HttpClientContext.create();
			String httpurl = "http://weibo.cn/1664677310/follow?vt=4&page="+page;
			CloseableHttpClient httpClient = HttpUtils.httpClient;
			httpContext.setCookieStore(HttpUtils.getCookie());
			HttpGet httpGet = new HttpGet(httpurl);
			httpGet.addHeader("User-Agent", HttpUserAgent.getUserAgent());
			httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
			httpGet.addHeader("Accept-Encoding", "gzip, deflate");
			httpGet.setConfig(config);
			try{
				CloseableHttpResponse httpResponse = httpClient.execute(httpGet, httpContext);
				String text = EntityUtils.toString(httpResponse.getEntity());
				Document document = Jsoup.parse(text);
				Elements elements = document.select("table");
				List<String> urls = new ArrayList<>();
				for(Element element : elements){
					String url = element.select("a").get(3).attr("href");
					urls.add(url);
				}
				httpGet.releaseConnection();
				return urls;
			}catch(Exception e){e.printStackTrace();}
		}
		Thread.sleep(1000);
		return null;
		
	}
	
	/**
	 * 取消关注的人
	 */
	public static void unFollow(String url){
		HttpGet httpGet = new HttpGet(url + "&rl=1&act=delc");
		RequestConfig config = RequestConfig.custom()
				//.setProxy(new HttpHost("127.0.0.1", 8888))
				.setCookieSpec(CookieSpecs.STANDARD)
				.setRedirectsEnabled(false)
				.build();
		CloseableHttpClient httpClient = HttpUtils.httpClient;
		HttpClientContext httpContext = HttpClientContext.create();
		httpContext.setCookieStore(HttpUtils.getCookie());
		httpGet.addHeader("User-Agent", HttpUserAgent.getUserAgent());
		httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
		httpGet.addHeader("Accept-Encoding", "gzip, deflate");
		httpGet.addHeader("refer", url);
		httpGet.setConfig(config);
		try{
			httpClient.execute(httpGet, httpContext);
			httpGet.releaseConnection();
		}catch(Exception e){e.printStackTrace();}
	}
	
	/**
	 * 关注用户
	 */
	public static Integer followUser(String addUrl){
		CloseableHttpClient httpClient = HttpUtils.httpClient;
		HttpClientContext httpContext = HttpClientContext.create();
		httpContext.setCookieStore(HttpUtils.getCookie());
		HttpGet httpGet = new HttpGet(addUrl);
		httpGet.addHeader("User-Agent", HttpUserAgent.getUserAgent());
		httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
		httpGet.addHeader("Accept-Encoding", "gzip, deflate");
		httpGet.addHeader("Referer", "http://weibo.cn/search/?pos=search&vt=4");
		RequestConfig config = RequestConfig.custom()
				//.setProxy(new HttpHost("127.0.0.1", 8888))
				.setCookieSpec(CookieSpecs.STANDARD)
				.setRedirectsEnabled(false)
				.build();
		httpGet.setConfig(config);
		try{
			CloseableHttpResponse httpResponse = httpClient.execute(httpGet, httpContext);
			return httpResponse.getStatusLine().getStatusCode();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			httpGet.releaseConnection();
		}
		return -1;
	}
	
	public static void main(String[] args) throws InterruptedException {
		MyWbFollows.getFollows(3);
	}
	
}
